package v1

import (
	"blog/errmsg"
	"blog/model"
	"blog/utils"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func AddArt(c *gin.Context){
	var article model.Article
	if err:=c.ShouldBindJSON(&article);err!=nil{
		utils.HttpResponse(c,http.StatusOK,errmsg.FAIL,"",nil)
	}else {
		code,msg := article.CreateArt()
		utils.HttpResponse(c,http.StatusOK,code,msg,nil)
	}
}
func GetArt(c *gin.Context){
	id,_:= strconv.Atoi(c.Param("id"))
	var article model.Article
	code,msg,art := article.GetArt(id)
	utils.HttpResponse(c,http.StatusOK,code,msg,art)
}
func GetArts(c *gin.Context){
	page,_ := strconv.Atoi(c.Query("page"))
	pagesize,_ := strconv.Atoi(c.Query("pagesize"))
	if page == 0{
		page = -1
	}
	if pagesize == 0{
		pagesize = -1
	}
	var article  model.Article
	code,msg,articles,total := article.GetArts(page,pagesize)
	utils.HttpResponseTotal(c,http.StatusOK,code,msg,articles,total)
}
func GetArtsByCateId(c *gin.Context){
	page,_ := strconv.Atoi(c.Query("page"))
	pagesize,_ := strconv.Atoi(c.Query("pagesize"))
	cid,_ := strconv.Atoi(c.Query("cid"))
	if page == 0{
		page = -1
	}
	if pagesize == 0{
		pagesize = -1
	}
	var article  model.Article
	code,msg,articles := article.GetArtsByCateId(cid,page,pagesize)
	utils.HttpResponse(c,http.StatusOK,code,msg,articles)
}
func UpdateArt(c *gin.Context){
	var article model.Article
	if err := c.ShouldBindJSON(&article);err!=nil{
		utils.HttpResponse(c,http.StatusOK,errmsg.FAIL,"",nil)
	}else {
		code,msg := article.UpdateArt()
		utils.HttpResponse(c,http.StatusOK,code,msg,nil)
	}
}
func DeleteArt(c *gin.Context){
	id,_:=strconv.Atoi(c.Query("id"))
	var article model.Article
	code,msg := article.DeleteArt(id)
	utils.HttpResponse(c,http.StatusOK,code,msg,nil)
}
